if (!Skink.Themes) Skink.Themes = {};

// placeholder for themes object
Skink.Themes.themes = {};

// initializer
Skink.Themes.init = function() {
    air.trace('Initialising themes...');
    Skink.Themes.themes = Skink.Themes.getThemePaths();
    for (x in Skink.Themes.themes) {
        $('head').append('<link href="' + Skink.Themes.themes[x].themecss + '" title="' + Skink.Themes.themes[x].themename + '" rel="stylesheet" type="text/css" />');
    }

    // build the dropdown menu
    $('#theme-basetheme').empty();

    /**
	* Styleswitch stylesheet switcher built on jQuery
	* Under an Attribution, Share Alike License
	* By Kelvin Luck ( http://www.kelvinluck.com/ )
	**/
    $('link[rel*=style][title]').each(function(i) {
        var title = this.getAttribute('title');
        $('#theme-basetheme').append('<option value="' + title + '">' + title + '</option>');
        Skink.dump("css:" + this.title);
    });

    $('#theme-basetheme').val(Skink.Prefs.get('theme-basetheme'));
    Skink.Themes.setCurrentTheme();

    // make the element to contain user CSS
    $('head').append('<style type="text/css" media="screen" id="UserCSSOverride"></style>');

    // load the user.css file
    Skink.Themes.loadUserCSS();
};

Skink.Themes.browseForUserCss = function() {
    var cssFilter = new air.FileFilter("StyleSheets", "*.css;");
    var userFile = new air.File();
    userFile.browseForOpen("Choose a CSS file", [cssFilter]);
    userFile.addEventListener(air.Event.SELECT, Skink.Themes.userCSSSelected);
}

Skink.Themes.userCSSSelected = function(event) {
    Skink.dump(event.target.url);
    var stylestr = Skink.Themes.loadUserStylesFromURL(event.target.url)
    Skink.dump(stylestr);
    Skink.Themes.setUserStyleSheet(stylestr, event.target.url);
}

Skink.Themes.setUserStyleSheet = function(stylestr, url) {
    Skink.Prefs.set('theme-userstylesheet', url);
    $('#UserCSSOverride').text(stylestr);
    // save the userstylesheet to the user's css file
    var csspath = Skink.Themes.getUserCSSFile().url;
    Skink.Sys.setFileContents(csspath, stylestr);
}

Skink.Themes.loadUserCSS = function() {
    var usercssfile = Skink.Themes.getUserCSSFile();
    if (usercssfile.exists) {
        $('#UserCSSOverride').text(Skink.Themes.loadUserStylesFromURL(usercssfile.url));
    }
};

Skink.Themes.getUserCSSFile = function() {
    return air.File.applicationStorageDirectory.resolvePath('user.css')
};

Skink.Themes.loadUserStylesFromURL = function(fileurl) {
    return Skink.Sys.getFileContents(fileurl);
}

Skink.Themes.clearUserStyleSheet = function() {
    Skink.Prefs.set('theme-userstylesheet', '');
    $('#UserCSSOverride').text('');
}

/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
Skink.Themes.setCurrentTheme = function() {
    Skink.dump('current theme:' + Skink.Prefs.get('theme-basetheme'));
    $('link[rel*=style][title]').each(function(i) {
        this.disabled = true;
        Skink.dump(this.getAttribute('title') + " is now disabled");
        if (this.getAttribute('title') == Skink.Prefs.get('theme-basetheme')) {
            this.disabled = false;
            Skink.dump(this.getAttribute('title') + " is now enabled");
        }
    });

    // change the paths for embedded imgs
    $('img.tab-icon, #loading img, .status-actions img').each(function(i) {
        this.src = this.src.replace(/\{theme-dir\}/, Skink.Prefs.get('theme-basetheme'));
    });
}

Skink.Themes.getThemePaths = function() {
    var appdir = air.File.applicationDirectory;
    var themesdir = appdir.resolvePath('themes');
    var appStore = air.File.applicationStorageDirectory;
    var userthemesdir = appStore.resolvePath('userthemes');

    // we load from both the built-in themes dir and the userthemes dir
    var list = themesdir.getDirectoryListing().concat(userthemesdir.getDirectoryListing())

    var themes = new Array();
    for (i = 0; i < list.length; i++) {
        if (list[i].isDirectory) {
            var thisthemedir = list[i];
            var thisthemename = thisthemedir.name;
            var thisthemecss = thisthemedir.resolvePath('theme.css');
            var thisthemejs = thisthemedir.resolvePath('theme.js');
            var thisthemeinfo = thisthemedir.resolvePath('info.js');

            var thistheme = {
                themename: thisthemename,
                themedir: thisthemedir.url,
                themecss: thisthemecss.url,
                themejs: thisthemejs.url,
                themeinfo: thisthemeinfo.url
            }

            // sanity check to make sure the themedir actually has something in it
            if (thisthemecss.exists) {
                themes.push(thistheme);
            }
        }
    }

    return themes;
}

Skink.Themes.getPathByName = function(themename) {
    for (i = 0; i < Skink.Themes.themes.length; i++) {
        if (Skink.Themes.themes[i].themename == themename) {
            return Skink.Themes.themes[i].themedir;
        }
    }
    return false;
}